package com.web.expspringboot.controller;

import com.web.expspringboot.entity.ProductSt;
import com.web.expspringboot.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService ps;

    @PostMapping("/uploadProduct")
    public boolean uploadProduct(@RequestBody ProductSt product) {
        return ps.uploadProduct(product);
    }


    @PostMapping("/updateProduct")
    public int updateProduct(@RequestBody ProductSt product) {
        return ps.updateProduct(product);
    }


    @GetMapping("/getAllProducts")
    public List<ProductSt> getAllProducts(@Param("startIndex") int startIndex, @Param("length") int length) {
        return ps.getAllProducts(startIndex, length);
    }

    @GetMapping("/getProductsByOwnerId")
    public List<ProductSt> getProductsByOwnerId(@Param("startIndex") int startIndex, @Param("length") int length) {
        return ps.getProductsByOwnerId(startIndex, length);
    }

    @GetMapping("/getProductById")
    public ProductSt getProductById(@RequestParam("productId") long productId) {
        return ps.getProductById(productId);
    }

    @GetMapping("/getProductsByTime")
    public List<ProductSt> getProductsByTime(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        Date dateStartTime = new Date(startTime);
        Date dateEndTime = new Date(endTime);
        return ps.getProductsByTime(dateStartTime, dateEndTime);
    }

    @GetMapping("/deleteProductById")
    public boolean deleteProductById(@RequestParam("productId") long productId){
        return ps.deleteProduct(productId);
    }

    @PostMapping("/resell")
    public boolean resell(@RequestParam("productId") long productId){
        return ps.resell(productId);
    }

}